package cn.tedu.wms.passport.service.impl;

import cn.tedu.wms.passport.ex.ServiceException;
import cn.tedu.wms.passport.mapper.PermissionMapper;
import cn.tedu.wms.passport.pojo.dto.PermissionAddNewDTO;
import cn.tedu.wms.passport.pojo.entity.Permission;
import cn.tedu.wms.passport.pojo.vo.PermissionListVO;
import cn.tedu.wms.passport.pojo.vo.PermissionStandardVO;
import cn.tedu.wms.passport.service.IPermissionService;
import cn.tedu.wms.passport.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理权限数据的业务类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    public PermissionServiceImpl() {
        log.info("创建业务对象: PermissionServiceImpl");
    }

    @Override
    public void add(PermissionAddNewDTO permissionAddNewDTO) {
        log.debug("开始处理添加权限业务,参数: {}",permissionAddNewDTO);

        log.debug("即将开始查询权限名称是否存在....");
        String name=permissionAddNewDTO.getName();
        int countName=permissionMapper.countByName(name);
        if(countName>=1){
            String message="添加权限失败,此权限已存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        log.debug("即将开始查询权限值名称是否存在....");
        String value=permissionAddNewDTO.getValue();
        int countValue=permissionMapper.countByValue(value);
        if(countValue>=1){
            String message="添加权限失败,权限值已存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Permission permission = new Permission();
        BeanUtils.copyProperties(permissionAddNewDTO,permission);
        log.debug("即将插入的权限数据: {}",permission);
        int row = permissionMapper.insert(permission);
        if(row<1){
            String message="添加权限失败,服务器忙,请稍候重试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除权限业务,参数: {}",id);

        log.debug("即将开始检查此权限是否存在....");
        PermissionStandardVO queryResult = permissionMapper.getById(id);
        if(queryResult==null){
            String message="删除权限失败,此权限不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //TODO 需要检查此权限是否关联了角色

        int row = permissionMapper.deleteById(id);
        if(row<1){
            String message="删除权限失败,服务器忙,稍后重试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public PermissionStandardVO getById(Long id) {
        log.debug("即将开始处理查询权限详情业务,参数: {}",id);
        PermissionStandardVO permissionStandardVO = permissionMapper.getById(id);
        if(permissionStandardVO==null){
            String message="查询权限详情失败,此权限不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return permissionStandardVO;
    }

    @Override
    public List<PermissionListVO> list() {
        log.debug("即将开始处理查询权限列表的业务");
        List<PermissionListVO> list=permissionMapper.list();
        return list;
    }
}
